package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.model.T_employee;
import com.cn.jdia.tas.service.IAttrecordService;
import com.cn.jdia.tas.service.IEmployeeService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class PersonRecordController {

	private static final String pattern = "yyyy-MM-dd";

	@Resource
	private IEmployeeService service = null;

	@Resource
	private IAttrecordService attService = null;

	@RequestMapping("/p_info_rec")
	public String getDailyData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = map.get("id");

		// 查询人员信息
		T_employee employee = service.getEmployeeInfoById(id);
		String deptname = employee.getDepatment().getDeptname();

		Map<String, Object> dmap = new HashMap<>();
		dmap.put("dept", deptname);

		// 查询7工作日内出勤
		DateTime nowDate = new DateTime();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		int count = 0;
		//for (DateTime tmp = nowDate.minusDays(1); count < 7; tmp = tmp.minusDays(1)) {
		for (DateTime tmp = nowDate; count < 7; tmp = tmp.minusDays(1)) {
			String day = tmp.toString(pattern);
			String nday = tmp.plusDays(1).toString(pattern);

			boolean weekendAndNoWork = false;
			int dayOfWeek = tmp.getDayOfWeek();

			List<T_attrecord> record = attService.getDetailAttrecords(id, day, day);
			String sin = "";
			String sout = "";
			for (int i = 0; i < record.size(); i++) {
				T_attrecord att = record.get(i);
				String tmpTime = att.getChecktime();

				if ((sin == "") || (sin == null)) {
					sin = tmpTime;
				} else {
					if (tmpTime.compareTo(sout) > 0) {
						sout = tmpTime;
					}
				}
			}

			if ((sin == "") && (sout == "") && ((dayOfWeek == 6) || (dayOfWeek == 7)))
				weekendAndNoWork = true;

			if (weekendAndNoWork) {
				continue;
			}

			System.out.println(day + " | " + sin + "  |  " + sout);
			Map<String, String> tmap = new HashMap<String, String>();
			tmap.put("date", day);
			tmap.put("beg", sin);
			tmap.put("end", sout);

			list.add(tmap);

			count++;
		}

		dmap.put("list", list);
		String retDate = gson.toJson(dmap);
		System.out.println(retDate);

		out.write(retDate);
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/onoffdutyrcd")
	public String getOnOffDutyRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = map.get("id");
		String day = map.get("date");

		List<T_attrecord> record = attService.getDetailAttrecords(id, day, day);
		Map<String, Object> rmap = new HashMap<>();
		rmap.put("rcdnum", record.size());
		if (record.size() != 0) {
			rmap.put("start", record.get(0).getChecktime());
			rmap.put("end", record.get(record.size() - 1).getChecktime());
		}

		out.write(gson.toJson(rmap));
		out.flush();
		out.close();

		return null;
	}
	
	//check person if he has attendance record
	@RequestMapping("/checkhasattendance")
	public String checkHasAttendance(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = map.get("id");
		String date = map.get("date");
		List<T_attrecord> record = attService.getDetailAttrecords(id, date, date);
		String count=null;
		if(record.size()==0){
			count="0";
		}else{
			count="1";
		}
		
		out.write(count);
		out.flush();
		out.close();
		
		return null;
	}
	
}
